package gpl.scotlandyard.ui.tools;

import gpl.scotlandyard.utils.I18n;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Provides somes tools methods.
 * @author Norbert Martin
 */
public abstract class ToolsFactory {

  /**
   * Creates an add,modify,delete popupmenu on the view.
   * @param model
   * @param view
   * @param addAl action
   * @param modAl action
   * @return JPopupMenu
   */
  public static JPopupMenu createAMDJPopupMenuOnList(final DefaultListModel model, final JList view, ActionListener addAl,
      ActionListener modAl) {
    JPopupMenu popup = new JPopupMenu();
    view.setToolTipText(I18n.get("TT_LIST"));

    final JMenuItem add = new JMenuItem(I18n.get("ADD"));
    add.addActionListener(addAl);

    final JMenuItem mod = new JMenuItem(I18n.get("MODIFY"));
    mod.addActionListener(modAl);

    final JMenuItem del = new JMenuItem(I18n.get("DELETE"));
    del.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        int result = JOptionPane.showConfirmDialog(null, I18n.get("R_U_SURE"), I18n.get("SY"), JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
          model.remove(view.getSelectedIndex());
        }
      }
    });

    popup.add(add);
    popup.add(mod);
    popup.add(del);

    if (model.getSize() == 0 || view.getSelectedIndex() == -1) {
      del.setEnabled(false);
      mod.setEnabled(false);
    }

    view.addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e) {
        del.setEnabled(view.getSelectedIndex() != -1);
        mod.setEnabled(view.getSelectedIndex() != -1);
      }
    });

    return popup;
  }

  /**
   * Default add, modify, delete popupmenu.
   * <p>Add and modify just check entry is not empty.</p>
   * @param model
   * @param view
   * @return JPopupMenu
   */
  public static JPopupMenu createAMDJPopupMenuOnList(final DefaultListModel model, final JList view) {
    ActionListener addAl = new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        String add = JOptionPane.showInputDialog(null, I18n.get("ADD"));
        if (!add.isEmpty()) {
          model.addElement(add);
        }
      }
    };
    ActionListener modAl = new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        String mod = JOptionPane.showInputDialog(null, I18n.get("MODIFY"), view.getSelectedValue());
        if (!mod.isEmpty()) {
          model.set(view.getSelectedIndex(), mod);
        }
      }
    };
    return createAMDJPopupMenuOnList(model, view, addAl, modAl);
  }

  /** Sets the passed dialog escape closable.
   * @param dial
   * @param act
   */
  public static void setEscapeClosable(JDialog dial, ActionListener act) {
    dial.getRootPane().registerKeyboardAction(act, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
  }
}
